package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.app.entity.ResidentialWorker;
import com.app.entity.User;
import com.app.entity.UserHome;
import com.app.exception.ValidateException;
import com.app.requestVO.StaffListRequestVO;
import com.app.requestVO.UserListRequestVO;
import com.app.responseVO.ReturnResponseVO;
import com.app.responseVO.ReturnResultResponseVO;
import com.app.responseVO.UserResponseVO;
import com.app.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;
import java.io.IOException;

/**
 * Created by xinjian.hu on 18-3-24.
 */
@Controller
public class StaffController extends BaseController {
    @Autowired
    private StaffService staffService;

    /**
     * 新增员工
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "staff/addStaff", method = RequestMethod.POST)
    public void addWork(@RequestBody String body,
                        HttpServletResponse response) throws IOException {
        ResidentialWorker residentialWorker = JSON.parseObject(body, ResidentialWorker.class);
        UserResponseVO userResponseVO = new UserResponseVO();
        try {
            staffService.addWork(residentialWorker);
            userResponseVO.setMsg("成功");
            userResponseVO.setStatue(true);
        } catch (ValidateException e) {
            userResponseVO.setMsg(e.getMsg());
            userResponseVO.setStatue(false);
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 更新员工信息
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "staff/updateStaff", method = RequestMethod.POST)
    public void updateStaff(@RequestBody String body,
                            HttpServletResponse response) throws IOException {
        ResidentialWorker residentialWorker = JSON.parseObject(body, ResidentialWorker.class);
        UserResponseVO userResponseVO = new UserResponseVO();
        try {
            staffService.updateWorker(residentialWorker);
            userResponseVO.setStatue(true);
            userResponseVO.setMsg("成功");
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("失败");
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 获取员工列表
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "staff/getStaffList", method = RequestMethod.POST)
    public void getStaffList(@RequestBody String body,
                             HttpServletResponse response) throws IOException {
        ReturnResultResponseVO<ResidentialWorker> returnResultResponseVO = new ReturnResultResponseVO<ResidentialWorker>();
        try {
            //PageVO pageVO = new PageVO();
            StaffListRequestVO staffListRequestVO = JSON.parseObject(body, StaffListRequestVO.class);
            if (null == staffListRequestVO || null == staffListRequestVO.getPageVO()) {
                throw new ValidationException("参数格式有误");
            }
            returnResultResponseVO = staffService.getStaffList(staffListRequestVO.getPageVO().getCurrentPage(), staffListRequestVO.getPageVO().getPagesize(), staffListRequestVO.getStaffType(), staffListRequestVO.getStaffName());
            returnResultResponseVO.setStatue(true);
        } catch (ValidationException e) {
            returnResultResponseVO.setStatue(false);
        }
        writeJSON(response, returnResultResponseVO);
    }

    /**
     * 获取员工详情
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "staff/getStaffDetail", method = RequestMethod.POST)
    public void getStaffDetail(@RequestBody String body,
                               HttpServletResponse response) throws IOException {
        ResidentialWorker residentialWorker = JSON.parseObject(body, ResidentialWorker.class);
        ResidentialWorker staffDetail = staffService.staffDetail(residentialWorker.getWorkerId());
        ReturnResponseVO<ResidentialWorker> returnResponseVO = new ReturnResponseVO<ResidentialWorker>();
        returnResponseVO.setData(staffDetail);
        returnResponseVO.setStatue(true);
        returnResponseVO.setMsg("成功");
        writeJSON(response, returnResponseVO);

    }
}
